package com.yan.test.work.testphoto

import android.os.Bundle
import android.view.LayoutInflater
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.permissionx.guolindev.PermissionX
import com.permissionx.guolindev.callback.RequestCallback
import com.yan.test.work.testphoto.databinding.ActivityMainBinding
import java.io.File

class MainActivity : AppCompatActivity() {
    val mbinding by lazy {
        ActivityMainBinding.inflate(LayoutInflater.from(this))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(mbinding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        mbinding.startTakePhoto.setOnClickListener{
            PermissionX.init(this)
                .permissions(arrayListOf("android.permission.CAMERA"
                    ,"android.permission.RECORD_AUDIO"
                    ,"android.permission.WRITE_EXTERNAL_STORAGE"
                ))
                .request(object: RequestCallback{
                    override fun onResult(
                        p0: Boolean,
                        p1: MutableList<String>,
                        p2: MutableList<String>
                    ) {

                        // TODO 如果是模拟器则无法打开照相机
                        val cameraType = if (false) CaptureActivity.CAMERA_TYPE_FRONT else CaptureActivity.CAMERA_TYPE_BACK
                        CaptureActivity.startActivityForResult(this@MainActivity, File(externalCacheDir , "123.mp4"), true, cameraType)
                    }

                })

        }
    }
}